package com.guashu.jaywayjsonpath.util;

import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import net.minidev.json.JSONArray;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DealMessageUtil {

    public static String safelyReadJsonPath(Object json, String path) {
        try {
            return JsonPath.read(json, path);
        } catch (PathNotFoundException e) {
            return null;
        }
    }


    public static String safelyReadJsonPathToString(Object json, String path) {
        try {
            Object result = JsonPath.read(json, path);

            // 检查结果是否为字符串
            if (result instanceof String) {
                return (String) result;
            } else if (result instanceof JSONArray) {
                JSONArray resultJsonArray= (JSONArray) result;
                // 如果结果是可迭代的，且只有一个元素，则返回该元素
                if(resultJsonArray.size()==1){
                    return  (String) resultJsonArray.get(0);
                }else{
                    throw new IllegalStateException("返回结果包含多个元素.");
                }
            }
        } catch (PathNotFoundException e) {
            // 如果路径不存在，则返回 null
            return null;
        }
        // 返回 null，表示无法获取字符串
        return null;
    }



    public static JSONArray safelyReadJsonPathToArray(Object json, String path) {
        try {
            // 尝试读取JSONArray
            return JsonPath.read(json, path);
        } catch (PathNotFoundException e) {
            // 路径不存在时返回null
            return null;
        }
    }
    public static Object safelyReadJsonPathReturnObject(Object json, String path) {
        try {
            // 尝试读取JSONArray
            return JsonPath.read(json, path);
        } catch (PathNotFoundException e) {
            // 路径不存在时返回null
            return null;
        }
    }


    /**
     * 截取二维数组JasonPath，"$.PDA.PD01[*].PD01E.PD01EH[*].PD01ER03" 返回 $.PDA.PD01[*]
     * @param input
     * @return
     */
    public static String getFirstArray(String input) {
        Pattern pattern = Pattern.compile("([^\\[\\]]+)\\[\\*\\]");
        Matcher matcher = pattern.matcher(input);

        if (matcher.find()) {
            String result = matcher.group(1);
            result = result + "[*]";
            return result;
        } else {
            return "No match found.";
        }
    }

    /**
     * 截取字符串 "$.PDA.PD01[*].PD01E.PD01EH[*].PD01ER03" 返回 $.PD01E.PD01EH[*]
     * @param input
     * @return
     */
    public static String getMidderArray(String input) {
        Pattern pattern = Pattern.compile("\\[\\*\\](.*?)\\[\\*\\]");
        Matcher matcher = pattern.matcher(input);
        if (matcher.find()) {
            String extracted = matcher.group(1);
            return "$" + extracted + "[*]";
        } else {
            return "No match found.";
        }
    }

    /**
     * 截取字符串"$.PDA.PD01[*].PD01E.PD01EH[*].PD01ER03" 返回 $.PD01ER03
     * @param input
     * @return
     */
    public static String getLast(String input) {
        String pattern = "\\[\\*\\]";

        // 编译正则表达式
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(input);

        int count = 0;
        int startPos = -1;

        // 找到第二个[*]的位置
        while (m.find()) {
            count++;
            if (count == 2) {
                startPos = m.end();
                break;
            }
        }

        // 如果找到了第二个[*]，则提取后面的字符
        if (startPos != -1) {
            String result = input.substring(startPos);
            result = "$" + result;
            return result;
        } else {
            return "未找到第二个[*]后面的字符。";
        }
    }

    /**
     * 是否包含两个[*]
     * @param str
     * @return
     */
    public static boolean containsTwoAsterisks(String str) {
        if (str.isEmpty()) {
            return false;
        }
        int count = 0;
        int index = -1;
        while ((index = str.indexOf("[*]", index + 1)) != -1) {
            count++;
        }
        return count == 2;
    }

    /**
     * 是否包含1个[*]
     * @param str
     * @return
     */
    public static boolean containsOneAsterisk(String str) {
        if (str.isEmpty()) {
            return false;
        }
        int count = 0;
        int index = -1;
        while ((index = str.indexOf("[*]", index + 1)) != -1) {
            count++;
        }
        return count == 1;
    }

    /**
     * 不包含[*]
     * @param str
     * @return
     */
    public static boolean notContainsAsterisk(String str) {
        if (str.isEmpty()) {
            return false;
        }
        return !str.contains("[*]");
    }

    /**
     * 截取 "$.PDA.PD01[*].PD01A.PD01AD01"返回 $.PDA.PD01[*]
     * @param input
     * @return
     */
    public static String getSubstringLeft(String input) {
        String delimiter = "[*]";
        int index = input.indexOf(delimiter);
        if (index != -1) {
            return input.substring(0, index + delimiter.length());

        } else {
            return input; // 如果未找到[*]，则返回原始字符串
        }
    }


    /**
     * 截取字符串"$.PDA.PD01[*].PD01A.PD01AD01" 返回 $.PD01A.PD01AD01
     * @param input
     * @return
     */
    public static String getSubstringRight(String input) {
        String delimiter = "[*]";
        int index = input.indexOf(delimiter);
        if (index != -1) {
            String str =input.substring(index + delimiter.length());
            return  "$"+str;
        } else {
            return ""; // 如果未找到分隔符，则返回空字符串或者您所需的默认值
        }
    }
}
